import Taro from '@tarojs/taro';

import {Command, GAME_TYPE, GAME_STORAGE_INFO, GAME_RPICE_TYPE, PROP_STAUTS} from '../constant';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';

import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },

    async status() {
      // 或取当前状态值
      let {
        main: {isLoading},
      } = getData();
      action.commonChange('main.isLoading', !isLoading);
    },

    /**
     * 查询用户信息
     */
    async loadUserInfo(payload) {
      console.log('action.ts loadUserInfo payload', payload);

      action.commonChange('main.userInfo', {});
      action.commonChange('main.response.loadUser', '');
      action.commonChange('main.status.loadingUser', true);

      let {data, code, message} = await api.customerBaseController.findCustomerBaseInfoForGame({});
      let userInfo: any = data || null;
      if (!userInfo) {
        userInfo = Taro.getStorageSync(GAME_STORAGE_INFO);
      }
      let accountType = '';
      if (userInfo && userInfo.customerName) {
        // 判断是否是会员
        if (!userInfo.isAquamarineMember && !userInfo.isCrystalMember) {
          accountType = '';
        } else if (userInfo.isAquamarineMember && userInfo.isCrystalMember) {
          accountType = 'isAquamarineMember&isCrystalMember';
        } else {
          accountType = userInfo.isAquamarineMember ? 'isAquamarineMember' : 'isCrystalMember';
        }
      }

      userInfo = {
        ...userInfo,
        accountType,
      };

      console.log('action.ts userinfo', userInfo);

      // 会员则去获取每日赠送游戏
      if (accountType) {
        action.loadGameTimesDaily({delFlag: null});
      }

      action.loadGameTimes({}); // 查询可用游戏次数

      action.commonChange('main.userInfo', {
        ...userInfo,
      });
      action.commonChange('main.response.loadUser', code);
      action.commonChange('main.status.loadingUser', false);
    },

    /**
     * 查询ip列表
     */
    async loadIpItems(payload) {
      console.log('action.tsx loadIpItems payload', payload);

      // action.commonChange('main.ipItems', []);
      action.commonChange('main.response.loadIp', '');
      action.commonChange('main.status.loadingIp', true);

      let {data, code, message} = await api.SyntheticCardController.getIpItems(payload);
      console.log('action.tsx loadIpItems data', data);

      action.commonChange('main.ipItems', data || []);
      action.commonChange('main.response.loadIp', code);
      action.commonChange('main.status.loadingIp', false);
    },

    /**
     * 查询游戏信息
     * @param payload
     */
    async loadGameInfo(payload) {
      action.commonChange('main.gameInfo', {});
      action.commonChange('main.response.loadGameInfo', '');
      action.commonChange('main.status.loadingGameInfo', true);

      // 编辑参数
      let params = {
        gameType: GAME_TYPE, //0-魔力金币雨，1-魔力抽盲盒，2-魔力赏，3-魔力抓娃娃，4-魔力抽抽卡，5-刮刮乐
      };

      let {data, code, message} = await api.SyntheticCardController.getGameIpConfigInfo(params);
      console.log('action.ts loadGameInfo data', data);

      let gameInfo = {};
      gameInfo = {
        ...data,
        text: data.detail ? data.detail : '',
      };

      action.commonChange('main.gameInfo', gameInfo);
      action.commonChange('main.response.loadGameInfo', code);
      action.commonChange('main.status.loadingGameInfo', false);
    },

    /**
     * 查询每日游戏次数领取
     * @param payload
     */
    async loadGameTimesDaily(payload) {
      action.commonChange('main.gameReceiveInfo', {});
      action.commonChange('main.response.loadGameDaily', '');
      action.commonChange('main.status.loadingGameDaily', true);

      // 编辑参数
      let params = {
        gameType: GAME_TYPE,
        ...payload,
      };

      let {data, code, message} = await api.SyntheticCardController.getGameTimesReceiveDaily(params);

      let result = data
        ? {
            ...payload,
            ...data,
          }
        : {
            ...payload,
          };

      action.commonChange('main.gameReceiveInfo', result);
      action.commonChange('main.response.loadGameDaily', code);
      action.commonChange('main.status.loadingGameDaily', false);
    },

    /**
     * 查询游戏次数
     * @param payload
     */
    async loadGameTimes(payload) {
      action.commonChange('main.response.loadGameTimes', '');
      action.commonChange('main.status.loadingGameTimes', true);

      // 编辑参数
      let params = {
        gameType: GAME_TYPE,
        ...payload,
      };

      let {data, code, message} = await api.SyntheticCardController.getGameTimesConfigInfo(params);
      console.log('index.tsx getGameTimes data', data);

      let {
        main: {userInfo},
      } = getData();

      userInfo = {
        ...userInfo,
        gameAvailableTimes: data || 0,
      };

      action.commonChange('main.userInfo', userInfo);
      action.commonChange('main.response.loadGameTimes', code);
      action.commonChange('main.status.loadingGameTimes', false);
    },
    /**
     * 查询任务列表
     */
    async loadTaskItems(payload) {
      action.commonChange('main.response.loadTask', '');
      action.commonChange('main.status.loadingTask', true);

      // 编辑参数
      let params = {
        ...payload,
        gameType: GAME_TYPE,
        // gameType: 0, // 暂时
      };

      let {data, code, message} = await api.GameTaskController.getTasksByGameType(params);
      console.log('loadTaskItems action data', data);

      let {
        main: {task},
      } = getData();

      action.commonChange('main.task', {
        ...task,
        items: data || [],
        page: {
          ...task.page,
        },
      });
      action.commonChange('main.response.loadTask', code);
      action.commonChange('main.status.loadingTask', false);
    },

    /**
     * 完成任务
     * @param payload
     */
    async computedTask(payload) {
      action.commonChange('main.response.computedTask', '');
      action.commonChange('main.status.computingTask', true);

      // 编辑参数
      let params = {
        ...payload,
      };
      let {data, code, message} = await api.GameTaskController.computedTask(params);

      action.commonChange('main.response.computedTask', code);
      action.commonChange('main.status.computingTask', false);
    },

    /**
     * 查询任务详情
     */
    async loadTaskDetailById(payload) {
      action.commonChange('main.response.loadTaskDetail', '');
      action.commonChange('main.status.loadingTaskDetail', true);

      // 编辑参数
      let params = {
        ...payload,
      };

      let {data, code, message} = await api.GameTaskController.getTaskDetailById(params);
      console.log('loadTaskItems action data', data);

      let {
        main: {task},
      } = getData();

      action.commonChange('main.task', {
        ...task,
        detail: {
          ...(data || {}),
        },
      });
      action.commonChange('main.response.loadTaskDetail', code);
      action.commonChange('main.status.loadingTaskDetail', false);
    },

    /**
     * 查询道具信息
     */
    async loadPropItems(payload) {
      action.commonChange('main.response.loadProp', '');
      action.commonChange('main.status.loadingProp', true);

      // 编辑参数
      let params = {
        // ...payload,
        prizeType: GAME_RPICE_TYPE['prop'],
        status: PROP_STAUTS['UNUSE'],
      };

      let {data, code, message} = await api.GamePropController.getMyProp(params);
      console.log('action.ts loadProp data', data);
      data = data || [];

      // 重组数据
      let result = [];
      if (data.length > 0) {
        data.map((i) => {
          i.propVO = i.propVO || {};
          result.push({
            ...i,
            propImg: i.propVO.imgUrl,
            propName: i.propVO.propName,
            propEndTime: i.propVO.endTime,
            propDesc: i.propVO.propDesc,
            propStartTime: i.propVO.startTime,
          });
        });
      }

      let {
        main: {prop},
      } = getData();

      action.commonChange('main.prop', {
        ...prop,
        items: result || [],
        page: {
          ...prop.page,
        },
      });
      action.commonChange('main.response.loadProp', code);
      action.commonChange('main.status.loadingProp', false);
    },

    /**
     * 判断能否使用道具
     * @param payload
     */
    async usePropPossibility(payload) {
      action.commonChange('main.codeMessage', '');
      action.commonChange('main.response.usePropPossibility', '');
      action.commonChange('main.status.usingPropPossibility', true);

      // 编辑参数
      let params = {
        ...payload,
        gameType: GAME_TYPE,
      };

      let {data, code, message} = await api.GamePropController.useProp(params);
      console.log('action.ts usePropPossibility data', data);

      action.commonChange('main.response.usePropPossibility', code);
      action.commonChange('main.codeMessage', message);
      action.commonChange('main.status.usingPropPossibility', false);
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageSyntheticCardIpPaMain'),
  };
}
